﻿$(function () {
    //Region
    $(".region").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '/AjaxQuery/SearchRegions', type: "POST", dataType: "json",
                data: {
                    term: request.term
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            value: item.Name, id: item.ID
                        };
                    }));
                }
            });
        },
        select: function (event, ui) {
            $("#hdnreg").val(ui.item.id);
        }
    });

    //District
    $(".district").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '/AjaxQuery/SearchDistricts', type: "POST", dataType: "json",
                data: {
                    term: request.term,
                    regionID: $("#hdnreg").val()
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            value: item.Name, id: item.ID
                        };
                    }));
                }
            });
        },
        select: function (event, ui) {
            $("#hdndist").val(ui.item.id);
        }
    });

    //City
    $(".city").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '/AjaxQuery/SearchCities', type: "POST", dataType: "json",
                data: {
                    term: request.term,
                    districtID: $("#hdndist").val()
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            value: item.Name, id: item.ID
                        };
                    }));
                }
            });
        },
        select: function (event, ui) {
            $("#hdncity").val(ui.item.id);

        }
    });

    //Street
    $(".street").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '/AjaxQuery/SearchStreets', type: "POST", dataType: "json",
                data: {
                    term: request.term,
                    cityID: $("#hdncity").val()
                },
                success: function (data) {
                    response($.map(data, function (item) {
                        return {
                            value: item.Name, id: item.ID
                        };
                    }));
                }
            });
        },
        minLength: 2
    });
    //-----------DatePicker------------*@
  $(function () {
            $(".datedoc").datepicker({
                 changeYear: true,
                  yearRange: '1920:'+new Date().toDateString(),
            });
        });
});